package zhoug.demo.zui.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import zhoug.common.adapters.recycler.layoutmanager.FlowLayoutManager;
import zhoug.common.interfaces.NameTransformer;
import zhoug.common.interfaces.OnRemoveListener;
import zhoug.common.utils.AppUtils;
import zhoug.common.utils.CollectionUtils;
import zhoug.common.utils.StringUtils;
import zhoug.demo.zui.Area;
import zhoug.demo.zui.DataGenerator;
import zhoug.demo.zui.R;
import zhoug.logging.Logger;
import zhoug.zui.input.ZUIInputEditText;
import zhoug.zui.input.ZUIInputMultiDateSelectView;
import zhoug.zui.input.ZUIInputSelector;
import zhoug.zui.input.ZUIInputTextView;
import zhoug.zui.list.OnSelectChangeListener;
import zhoug.zui.list.ZUISelectionListDialog;

/**
 * @Author: zhoug
 * @Date: 2024-07-08
 * @Description:
 */
public class ZUIInputAllActivity extends ZUIInputViewBaseActivity {
    private static final String TAG = ">>>ZUIInputAllActivity";
    private ZUISelectionListDialog<Area> mAreaDialog;
    private Switch switchEnable;
    private Switch switchError;
    private EditText editText1;
    private ZUIInputTextView inputTextView1;
    private ZUIInputTextView inputTextView2;
    private ZUIInputEditText inputEditTextView1;
    private ZUIInputMultiDateSelectView inputMultiDateSelector1;
    private ZUIInputMultiDateSelectView inputMultiDateSelector2;
    private ZUIInputMultiDateSelectView inputMultiDateSelector3;
    private ZUIInputMultiDateSelectView inputMultiDateSelectorYear;
    private ZUIInputMultiDateSelectView inputMultiDateSelectorYearMonth;
    private ZUIInputEditText inputEditTextView2;
    private ZUIInputSelector inputSelector1;
    private Switch switchMulti;
    private Switch switchTextSplitMode;
    private RadioButton rbtnFlow;
    private RadioButton rbtnH;
    private RadioButton rbtnV;
    private RadioButton rbtnGrid;
    private RadioButton rbtnStaggered;
    private RadioGroup radioGroup;
    private LinearLayout contentView;


    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_input_all);
        findViews();
//        printPadding();

        initialize();

    }

    private void initialize(){

        switchMulti.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mAreaDialog.setMultiSelect(isChecked);
                inputSelector1.setMultiSelect(isChecked);
                mAreaDialog.notifyDataChange(true);
            }
        });
        switchTextSplitMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                inputSelector1.setTextSplitMode(isChecked);
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rbtn_h :
                        inputSelector1.setLayoutManager(new LinearLayoutManager(ZUIInputAllActivity.this, RecyclerView.HORIZONTAL,false));
                        break;
                    case R.id.rbtn_v :
                        inputSelector1.setLayoutManager(new LinearLayoutManager(ZUIInputAllActivity.this));
                        break;
                    case R.id.rbtn_grid:
                        inputSelector1.setLayoutManager(new GridLayoutManager(ZUIInputAllActivity.this, 3));
                        break;
                    case R.id.rbtn_Staggered:
                        inputSelector1.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
                        break;
                    default:
                        inputSelector1.setLayoutManager(new FlowLayoutManager(true));
                        break;
                }
            }
        });

        //选择器
       mAreaDialog=new ZUISelectionListDialog<Area>(this) {

            @Override
            public String getItemName(Area itemData) {
                return itemData.getName();
            }
        };
        mAreaDialog.setData(DataGenerator.getAreas());
        mAreaDialog.setMultiSelect(false);
        mAreaDialog.setMaxHeight(AppUtils.getScreenSize().y/2);
        mAreaDialog.setOnSelectChangeListener(new OnSelectChangeListener<Area>() {
            @Override
            public void onSelectChange(@NonNull List<Area> select) {
                Logger.d(TAG, "onSelectChange:"+ CollectionUtils.listToNames(select));
                inputSelector1.setSelect(select);
            }

            @Override
            public void onClearSelect() {
                Logger.d(TAG, "onClearSelect:");
                inputSelector1.clearSelect();
            }
        });
        inputSelector1.setMultiSelect(mAreaDialog.isMultiSelect());
        inputSelector1.setSelect(mAreaDialog.getItemData(1));
//        inputSelector1.setSelects(Arrays.asList(mAreaDialog.getItemData(0),mAreaDialog.getItemData(1)));

        mAreaDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                inputSelector1.setExpandIconState(false);
            }
        });
        inputSelector1.setOnPopupListener(new ZUIInputSelector.OnPopupListener() {
            @Override
            public void onClick(ZUIInputSelector selector) {
                Logger.d(TAG, "onClick:点击选择");
                mAreaDialog.show();
            }
        });

        inputSelector1.setOnRemoveListener(new OnRemoveListener<NameTransformer>() {
            @Override
            public void onRemove(View view, NameTransformer nameTransformer) {
                Logger.d(TAG, "onRemove:"+nameTransformer.transformName());
                mAreaDialog.clearSelect((Area) nameTransformer);
                mAreaDialog.notifyDataChange(true);
            }
        });

        inputMultiDateSelectorYear.showMonth(false);
        inputMultiDateSelectorYear.showDay(false);


        inputMultiDateSelectorYearMonth.showDay(false);

    }



    private void printPadding(){
        contentView.postDelayed(()->{
            printPadding("editText1",editText1);
            printPadding("inputTextView1",inputTextView1.getValueView());
            printPadding("inputTextView2",inputTextView2.getValueView());
            printPadding("inputEditTextView1",inputEditTextView1.getValueView());
            printPadding("inputEditTextView2",inputEditTextView2.getValueView());
        }, 5000);
    }


    private void printPadding(String tag,TextView view){
        int paddingStart = view.getPaddingStart();
        int paddingTop = view.getPaddingTop();
        int paddingBottom = view.getPaddingBottom();
        int paddingEnd = view.getPaddingEnd();

        Logger.d(TAG, tag+"=>"+ StringUtils.format("paddingStart:%d,paddingEnd:%d,paddingTop:%d,paddingBottom:%d",paddingStart,paddingEnd,paddingTop,paddingBottom));

    }

    @Override
    protected ViewGroup getContentView() {
        return contentView;
    }

    @Override
    protected Switch getEnableSwitchView() {
        return switchEnable;
    }

    @Override
    protected Switch getErrorSwitchView() {
        return switchError;
    }


    private void findViews() {
        switchEnable = findViewById(R.id.switch_enable);
        switchError = findViewById(R.id.switch_error);
        editText1 = findViewById(R.id.editText1);
        inputTextView1 = findViewById(R.id.inputTextView1);
        inputTextView2 = findViewById(R.id.inputTextView2);
        inputEditTextView1 = findViewById(R.id.inputEditTextView1);
        inputMultiDateSelector1 = findViewById(R.id.inputMultiDateSelector1);
        inputMultiDateSelector2 = findViewById(R.id.inputMultiDateSelector2);
        inputMultiDateSelector3 = findViewById(R.id.inputMultiDateSelector3);
        inputMultiDateSelectorYear = findViewById(R.id.inputMultiDateSelectorYear);
        inputMultiDateSelectorYearMonth = findViewById(R.id.inputMultiDateSelectorYearMonth);
        inputEditTextView2 = findViewById(R.id.inputEditTextView2);
        inputSelector1 = findViewById(R.id.inputSelector1);
        switchMulti = findViewById(R.id.switch_multi);
        switchTextSplitMode = findViewById(R.id.switch_textSplitMode);
        rbtnFlow = findViewById(R.id.rbtn_flow);
        rbtnH = findViewById(R.id.rbtn_h);
        rbtnV = findViewById(R.id.rbtn_v);
        rbtnGrid = findViewById(R.id.rbtn_grid);
        rbtnStaggered = findViewById(R.id.rbtn_Staggered);
        radioGroup = findViewById(R.id.radioGroup);
        contentView = findViewById(R.id.contentView);
    }
}
